package app.caihan.scframe.util

import android.content.Context
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.SnackbarUtils
import org.jetbrains.anko.alert

/**
 * 权限请求工具类
 * 尽量不要在一进入页面就请求权限,尽可能的放到按钮点击的时候去校验
 *
 * @author Seven-C
 * @date 2020/8/2
 */
object PermissionHelper {

    fun request(
        context: Context,
        callback: PermissionUtils.SimpleCallback,
        snackbarStr: String,
        vararg permissions: String
    ) {
        PermissionUtils.permission(*permissions)
            .rationale { activity, shouldRequest -> showRationaleDialog(activity, shouldRequest) }
            .callback(object : PermissionUtils.SingleCallback {
                override fun callback(
                    isAllGranted: Boolean, granted: MutableList<String>,
                    deniedForever: MutableList<String>, denied: MutableList<String>
                ) {
                    LogUtils.dTag("PermissionHelper", isAllGranted, granted, deniedForever, denied)
                    if (isAllGranted) {
                        //全部允许
                        callback.onGranted()
                        return
                    }
                    if (deniedForever.isNotEmpty()) {
                        //被永久拒绝了
                        showOpenAppSettingDialog(context)
                        return
                    }
                    val activity = ActivityUtils.getActivityByContext(context)
                    if (activity != null) {
                        SnackbarUtils.with(activity.findViewById(android.R.id.content))
                            .setDuration(SnackbarUtils.LENGTH_LONG)
                            .setMessage(getSnackbarStr(snackbarStr, denied))
                            .showWarning()
                    }
                    callback.onDenied()
                }

                fun getSnackbarStr(snackbarStr: String, denied: MutableList<String>): String {
                    return if (snackbarStr.isNullOrEmpty()) "权限申请失败: ${permissions2String(denied)}" else snackbarStr
                }

                fun permissions2String(permissions: MutableList<String>): String {
                    if (permissions.isEmpty()) return "[]"
                    val sb: StringBuilder = StringBuilder()
                    for (permission in permissions) {
                        sb.append(", " + permission.substring(permission.lastIndexOf('.') + 1))
                    }
                    return "[${sb.substring(2)}]"
                }
            })
            .request()
    }

    fun showRationaleDialog(
        context: Context,
        shouldRequest: PermissionUtils.OnRationaleListener.ShouldRequest
    ) {
        context.alert("您已经拒绝我们申请授权，请您同意授权，否则功能无法正常使用!", "注意") {
            positiveButton("确定") { shouldRequest.again(true) }
            negativeButton("取消") { shouldRequest.again(false) }
        }.show()
    }

    fun showOpenAppSettingDialog(context: Context) {
        context.alert("我们需要的一些权限您拒绝或系统应用失败失败，请手动设置为页面授权，否则功能不能正常使用!", "注意") {
            positiveButton("确定") { PermissionUtils.launchAppDetailsSettings() }
            negativeButton("取消") { }
        }.show()
    }
}